package com.cloud.pole.shop.service.order.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloud.pole.api.advert.AdvertApi;
import com.cloud.pole.api.param.PicklistApi;
import com.cloud.pole.api.user.UserCollectionApi;
import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.util.jwt.IJWTInfo;
import com.cloud.pole.common.util.jwt.JwtTokenUtil;
import com.cloud.pole.feign.IAdvertService;
import com.cloud.pole.feign.IParamPickListService;
import com.cloud.pole.feign.IUserCollectionService;
import com.cloud.pole.shop.biz.goods.ShopGoodsBiz;
import com.cloud.pole.shop.biz.goods.ShopGoodsCategoryBiz;
import com.cloud.pole.shop.biz.goods.ShopGoodsImgBiz;
import com.cloud.pole.shop.biz.goods.ShopGoodsPromotionBiz;
import com.cloud.pole.shop.biz.goods.ShopGoodsSpecBiz;
import com.cloud.pole.shop.biz.goods.ShopGoodsValueBiz;
import com.cloud.pole.shop.biz.order.ShopOrderShoppingCartBiz;
import com.cloud.pole.shop.biz.shops.ShopShopsBiz;
import com.cloud.pole.shop.constant.ShopCommonConstant;
import com.cloud.pole.shop.model.entity.goods.ShopGoods;
import com.cloud.pole.shop.model.entity.goods.ShopGoodsCategory;
import com.cloud.pole.shop.model.entity.goods.ShopGoodsImg;
import com.cloud.pole.shop.model.entity.goods.ShopGoodsSpec;
import com.cloud.pole.shop.model.entity.goods.ShopGoodsValue;
import com.cloud.pole.shop.model.entity.order.ShopOrderShoppingCart;
import com.cloud.pole.shop.model.entity.shops.ShopShops;
import com.cloud.pole.shop.model.entity.shops.ShopShopsWithBLOBs;
import com.cloud.pole.shop.model.vo.IndexVo;
import com.cloud.pole.shop.model.vo.goods.GoodsDetailVo;
import com.cloud.pole.shop.model.vo.goods.ShopGoodsPromotionByTypeVo;
import com.cloud.pole.shop.model.vo.goods.ShopGoodsVo;
import com.cloud.pole.shop.model.vo.order.ShoppingCartGoodsVo;
import com.cloud.pole.shop.model.vo.order.ShoppingCartVo;
import com.cloud.pole.shop.model.vo.shops.ShopShopsVo;
import com.cloud.pole.shop.service.IndexService;
import com.cloud.pole.shop.service.goods.GoodsService;
import com.cloud.pole.shop.service.order.ShoppingCartService;
import com.github.pagehelper.StringUtil;

import cn.hutool.core.bean.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/*
*2019年1月30日
*cloud-user
*author:frank-chen
*/
@Slf4j
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService{
	@Autowired 
	private ShopOrderShoppingCartBiz shopOrderShoppingCartBiz;
	@Autowired 
	private GoodsService goodsService;
	@Autowired 
	private ShopGoodsBiz shopGoodsBiz;
	@Autowired 
	private ShopGoodsSpecBiz shopGoodsSpecBiz;
	@Autowired 
	private ShopShopsBiz shopShopsBiz;
	
	@Override
	public Integer addShoppingCart(String token, Integer specId, Integer productNum, Integer goodsId)
			throws Exception {
		// TODO Auto-generated method stub
		IJWTInfo jwtInfo = JwtTokenUtil.getInfoFromToken(token);
	    //检测specId、goodId是否有效
		ShopGoods shopGoods = shopGoodsBiz.selectByPrimaryKey(goodsId);
		if(!goodsService.checkShoppingGoods(shopGoods))
		{
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "产品无效");
		}
		ShopGoodsSpec shopGoodsSpec = shopGoodsSpecBiz.selectById(specId);
		if( !goodsService.checkGoodsSpec(shopGoodsSpec))
		{
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "规格无效");
		}
		//检测库存是否足够
		if(shopGoods.getStockStatus() == -1) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "库存不够");
		}
		if(shopGoodsSpec.getStockQuantity().intValue() < productNum ) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "库存不够");
		}
		//是否限制了支付方式
		if(shopGoods.getOnlyPayMethod() != ShopGoods.ONLY_PAY_METHOD_UNLIMT) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "付款方式限制，不能加入购物车");
		}
		//查询是否已经有相同
		ShopOrderShoppingCart searchMsg = new ShopOrderShoppingCart();
		searchMsg.setGoodsId(goodsId);
		searchMsg.setGoodsSpecId(specId);
		searchMsg.setUserId(Long.parseLong(jwtInfo.getId()));
		ShopOrderShoppingCart msg = shopOrderShoppingCartBiz.selectOne(searchMsg);
		if(msg != null) {
			msg.setProductNum(msg.getProductNum().intValue() + productNum);
			shopOrderShoppingCartBiz.updateSelectiveById(msg);
		}
		else {
			msg = new ShopOrderShoppingCart();
			msg.setCreateTime(new Date());
			msg.setGoodsId(goodsId);
			msg.setGoodsSpecId(specId);
			msg.setPlatformId("");
			msg.setProductNum(productNum);
			msg.setUserId(Long.parseLong(jwtInfo.getId()));
			msg.setUserName(jwtInfo.getUniqueName());
			msg.setShopId(shopGoods.getShopId());
			shopOrderShoppingCartBiz.insertSelective(msg);
		}
		//统计购物车订单数量
		 Example example = new Example(ShopOrderShoppingCart.class);
		 Criteria criteria = example.createCriteria();
		 criteria.andEqualTo("userId", Long.parseLong(jwtInfo.getId()));
		 int num = shopOrderShoppingCartBiz.selectCountByExample(example);
		 return num;
	}


	@Override
	public ShoppingCartVo getShoppingCartList(String token) throws Exception {
		// TODO Auto-generated method stub
		IJWTInfo jwtInfo = JwtTokenUtil.getInfoFromToken(token);
		//按店铺分类
		List<ShopOrderShoppingCart> shoppingCartList = shopOrderShoppingCartBiz.selectListByUserId(Long.parseLong(jwtInfo.getId()));
		ShoppingCartVo shoppingCartVo = new ShoppingCartVo();
		for (ShopOrderShoppingCart odercar : shoppingCartList) {
			ShoppingCartGoodsVo shoppingCartGoodsVo = new ShoppingCartGoodsVo();
			BeanUtils.copyProperties(odercar, shoppingCartGoodsVo);
			Integer specId = odercar.getGoodsSpecId();
			ShopGoodsSpec shopGoodsSpec = shopGoodsSpecBiz.selectById(specId);
			ShopGoods shopGoods = shopGoodsBiz.selectByPrimaryKey(odercar.getGoodsId());
			ShopGoodsVo ShopGoodsVo = new ShopGoodsVo();
			BeanUtil.copyProperties(shopGoods, ShopGoodsVo);
			shoppingCartGoodsVo.setShopGoods(ShopGoodsVo);
			shoppingCartGoodsVo.setShopGoodsSpec(shopGoodsSpec);
			shoppingCartVo.getShoppingCartGoodsVoList().add(shoppingCartGoodsVo);
			
			boolean hasShop = false;
			for (ShopShopsVo shopShopsVo : shoppingCartVo.getShopShopsVo()) {
				if (String.valueOf(shopShopsVo.getShopId()).equals(String.valueOf(odercar.getShopId()))) {
					hasShop = true;
					break;
				}
			}
			if (!hasShop) {
				ShopShopsWithBLOBs shopShops = shopShopsBiz.selectById(odercar.getShopId());
				ShopShopsVo shopShopsVo = new ShopShopsVo();
				BeanUtil.copyProperties(shopShops, shopShopsVo);
				shoppingCartVo.getShopShopsVo().add(shopShopsVo);
			}
		}
		return shoppingCartVo;
	}


	@Override
	public boolean delShoppingCart(String token, Integer cartId) throws Exception {
		 // TODO Auto-generated method stub
		 IJWTInfo jwtInfo = JwtTokenUtil.getInfoFromToken(token);

	
		 ShopOrderShoppingCart msg = new ShopOrderShoppingCart();
		 msg.setUserId(Long.parseLong(jwtInfo.getId()));
		 msg.setCartId(cartId);
		 ShopOrderShoppingCart shopOrderShoppingCart = shopOrderShoppingCartBiz.selectOne(msg);
		 if(shopOrderShoppingCart == null) {
			 throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "cartId错误");
		 }
		 shopOrderShoppingCartBiz.delete(shopOrderShoppingCart);
		 return true;
	}
	
	
}
